﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Antioch.Base;
using Microsoft.Xna.Framework.Audio;


namespace Antioch
{
    public class Main : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
		SpriteFont Font;

       

		public Main()
		{
            Screen.CommandLineArguments = new _CommandLineArguments();

#if !XBOX
            var ArgsType = typeof(_CommandLineArguments);

            String[] Arguments = Environment.GetCommandLineArgs();
            for (int i = 1; i < Arguments.Length; )
            {
                String ArgName = Arguments[i];
                ++i;

                if (ArgName[0] == '-')
                {
                    ArgName = ArgName.Substring(1);

                    var Property = ArgsType.GetProperty(ArgName);
                    if (Property == null)
                    {
                        //Raise error
                        continue;
                    }

                    if (i >= Arguments.Length) continue;
                    Property.SetValue(Screen.CommandLineArguments,
                        System.Convert.ChangeType(Arguments[i], Property.PropertyType), null);
                    ++i;
                }
                else if (ArgName[0] == '+')
                {
                    ArgName = ArgName.Substring(1);

                    if (i >= Arguments.Length) continue;
                    int Value = System.Convert.ToInt32(Arguments[i]);
                    Screen.CommandLineArguments.SaveStateOverrides.Add(ArgName, Value);
                    ++i;
                }
                else
                    continue;

            }
#endif

			graphics = new GraphicsDeviceManager(this);

#if XBOX360
            graphics.IsFullScreen = true;
            graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
            graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
#else
			graphics.PreferredBackBufferWidth = Screen.CommandLineArguments.Width;
			graphics.PreferredBackBufferHeight = Screen.CommandLineArguments.Height;
			graphics.IsFullScreen = false;
#endif

			IsFixedTimeStep = true;
			TargetElapsedTime = System.TimeSpan.FromTicks(System.TimeSpan.TicksPerSecond / 60);

#if XBOX360
            Components.Add(new GamerServicesComponent(this));
#endif
			Components.Add(new FrameRateCounter(this));

		}

        private EventBasedInput input = new EventBasedInput();
       
        protected override void Initialize()
        {
            base.Initialize();

            input.ButtonEvents = ButtonEvent;
            input.StickEvents = StickEvent;
            input.TriggerEvents = TriggerEvent;
            input.BindKey(Microsoft.Xna.Framework.Input.Keys.Enter, EventBasedInput.Buttons.Start);
            input.BindKeyStick(EventBasedInput.Sticks.Left, Keys.W, Keys.S, Keys.A, Keys.D);
            input.BindKey(Keys.Space, EventBasedInput.Buttons.A);
            input.BindKey(Keys.LeftControl, EventBasedInput.Buttons.Y);
            input.BindKey(Keys.X, EventBasedInput.Buttons.X);
            input.BindMouseButton(EventBasedInput.MouseButtons.Left, EventBasedInput.Buttons.DNU_RIGHTTRIGGER);
            input.BindKey(Keys.U, EventBasedInput.Buttons.DPadUp);

			Screen.GraphicsDevice = GraphicsDevice;
            Screen.GraphicsDeviceManager = this.graphics;
			Screen.Parent = this;
			Screen.SpriteBatch = new SpriteBatch(GraphicsDevice);
			Screen.Components = Components;
			Screen.Game = this;
            Screen.Input = input;
	    }

        public void ButtonEvent(PlayerIndex index, EventBasedInput.Buttons button, EventBasedInput.EventTypes type, TimeSpan duration)
        {
			Screen.TopScreen.ButtonEvent(index, button, type, duration);
        }

        public void StickEvent(PlayerIndex index, EventBasedInput.Sticks stick, EventBasedInput.EventTypes type, Vector2 delta, TimeSpan duration)
        {
			Screen.TopScreen.StickEvent(index, stick, type, delta, duration);
        }

        public void TriggerEvent(PlayerIndex index, EventBasedInput.Triggers trigger, EventBasedInput.EventTypes type, float delta, TimeSpan duration)
        {
			Screen.TopScreen.TriggerEvent(index, trigger, type, delta, duration);
        }

        protected override void LoadContent()
        {
			Screen.Content = Content;
			Screen.PushScreen(new ScreenOpeningSplash());
			Font = Content.Load<SpriteFont>("Antioch.Content/Console");
        }


        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
#if XBOX360
			if (!Guide.IsVisible && IsActive)
			{
#endif
				input.Update(gameTime, PlayerIndex.One);
#if XBOX360
				input.Update(gameTime, PlayerIndex.Two);
				input.Update(gameTime, PlayerIndex.Three);
				input.Update(gameTime, PlayerIndex.Four);
#endif

				Screen.TopScreen.Update(gameTime);
#if XBOX360
			}
#endif

			base.Update(gameTime);
		}

        protected override void Draw(GameTime gameTime)
        {
			GraphicsDevice.Clear(Color.Black);
            foreach (Screen screen in Screen.Screens) screen.Draw();

			base.Draw(gameTime);
        }
    }

    static class Program
    {  
        static void Main()
        {
            using (Main game = new Main())
            {
                game.Run();
            }
        }
    }
}
